"""
Case Type   : bugfix
Case Name   : 主备模式，备机设置可写，备机写转发处理gsql -c方式执行的一个字符串含连续的多个sql语句
Create At   : 2025/6/12
Owner       : liu-tong-8848
Description :
    1、设置enable_renote_excute为on，开启允许备机执行写语句
    2、gsql -c执行建表语句
    3、gsql -c执行多条sql语句
    4、gsql -c执行查询语句
    5、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:设置enable_renote_excute为on，开启允许备机执行写语句；expect:成功----'
        self.log.info(text)
        result = self.sh_primary.execute_gsguc("set", self.constant.GSGUC_SUCCESS_MSG, "enable_remote_execute = on")
        self.assertTrue(result)
        self.sh_primary.restart_db_cluster()

        text = '----step2:gsql -c执行建表语句；expect:成功----'
        self.log.info(text)
        cmd1 = f'source {macro.DB_ENV_PATH}; gsql -d {self.user_node.db_name} -p {self.user_node.db_port} -r -t -c ' \
               + f'"drop table if exists t_fixed_115;"'
        self.log.info(cmd1)
        cmd1_res = self.user_node.sh(cmd1).result()
        self.assertNotIn('ERROR', cmd1_res, '执行失败' + text)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, cmd1_res, '执行失败' + text)

        cmd2 = f'source {macro.DB_ENV_PATH}; gsql -d {self.user_node.db_name} -p {self.user_node.db_port} -r -t -c ' \
               + f'"create table t_fixed_115(id int, name varchar(300));"'
        self.log.info(cmd2)
        cmd2_res = self.user_node.sh(cmd2).result()
        self.assertNotIn('ERROR', cmd2_res, '执行失败' + text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, cmd2_res, '执行失败' + text)

        text = '----step3:gsql -c执行多条sql语句；expect:成功----'
        self.log.info(text)
        cmd = f'source {macro.DB_ENV_PATH}; gsql -d {self.user_node.db_name} -p {self.user_node.db_port} -r -t -c ' \
              + f'"begin;insert into t_fixed_115 select n, \'z\' from generate_series(1,999) n; commit;"'
        self.log.info(cmd)
        cmd_res = self.user_node.sh(cmd).result()
        self.assertNotIn('ERROR', cmd_res, '执行失败' + text)
        self.assertIn(self.constant.COMMIT_SUCCESS_MSG, cmd_res, '执行失败' + text)

        text = '----step4:gsql -c执行查询语句；expect:成功----'
        self.log.info(text)
        cmd = f'source {macro.DB_ENV_PATH}; gsql -d {self.user_node.db_name} -p {self.user_node.db_port} -r -t -c ' \
              + f'"select count(*) from t_fixed_115;"'
        self.log.info(cmd)
        cmd_res = self.user_node.sh(cmd).result()
        self.assertNotIn('ERROR', cmd_res, '执行失败' + text)
        self.assertIn('999', cmd_res, '执行失败' + text)

    def tearDown(self):
        text = '----step5:清理环境；expect:成功----'
        self.log.info(text)
        cmd = f'source {macro.DB_ENV_PATH}; gsql -d {self.user_node.db_name} -p {self.user_node.db_port} -r -t -c ' \
              + f'"drop table if exists t_fixed_115;"'
        self.log.info(cmd)
        cmd_res = self.user_node.sh(cmd).result()
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, cmd_res, '执行失败' + text)
        result = self.sh_primary.execute_gsguc("set", self.constant.GSGUC_SUCCESS_MSG, "enable_remote_execute = off")
        self.assertTrue(result)
        self.sh_primary.restart_db_cluster()
        self.log.info(f'----{os.path.basename(__file__)} end-----')
